package com.alan.wifishare.view.impl.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.alan.libapi.model.business.SharedWifiInfo;
import com.alan.library.allog.KLog;
import com.alan.wifishare.R;
import com.alan.wifishare.presenter.MainSharePS;
import com.alan.wifishare.view.impl.ActivityJumpUtil;
import com.alan.wifishare.view.iview.IVMainShareFM;
import com.alan.wifishare.view.ui.SHAMySharedWifiAdapter;

import java.util.List;

/**
 * @className: MainShareFragment
 * @describtion: MainShareFragment
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/20 18:38
 */
public class MainShareFragment extends Fragment implements IVMainShareFM, View.OnClickListener {

    private MainSharePS mainSharePS;
    private Button shaMySharedRefreshBtn;
    private Button shaMySharedAddWifiBtn;
    private ListView shaMySharedWifiListview;
    private SHAMySharedWifiAdapter shaMySharedWifiAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main_share_layout, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mainSharePS = new MainSharePS(this);
        initView();
        mainSharePS.getMySharedWifiList();
    }

    private void initView(){
        shaMySharedRefreshBtn = (Button) getView().findViewById(R.id.shaMySharedRefreshBtn);
        shaMySharedAddWifiBtn = (Button) getView().findViewById(R.id.shaMySharedAddWifiBtn);
        shaMySharedRefreshBtn.setOnClickListener(this);
        shaMySharedAddWifiBtn.setOnClickListener(this);
        shaMySharedWifiListview = (ListView) getView().findViewById(R.id.shaMySharedWifiListview);
        shaMySharedWifiAdapter = new SHAMySharedWifiAdapter(getContext());
        shaMySharedWifiListview.setAdapter(shaMySharedWifiAdapter);
        shaMySharedWifiListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                SharedWifiInfo sharedWifiInfo = shaMySharedWifiAdapter.getItemList().get(i);
                toastMsg(sharedWifiInfo.getWifiSSID());
                ActivityJumpUtil.jump2SHAWifiSubmitActToUpdateWifi(getActivity(), sharedWifiInfo.getWifiId(), sharedWifiInfo.getWifiSSID(),sharedWifiInfo.getWifiPassword(),
                        sharedWifiInfo.getWifiMac(), sharedWifiInfo.getWifiAreaId(), sharedWifiInfo.getWifiLongitude(), sharedWifiInfo.getWifiLatitude(),sharedWifiInfo.getWifiAddress());
            }
        });
    }

    @Override
    public void showSharedWifiList(List<SharedWifiInfo> wifiInfoList) {
        shaMySharedWifiAdapter.setItemList(wifiInfoList);
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void toastMsg(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        KLog.e("requestCode = " + requestCode);
        KLog.e("resultCode = " + resultCode);
        if(requestCode == ActivityJumpUtil.RequestCode.SHA_WIFI_SHARE_2_SUBMIT){
            //从更新Wifi处返回
        }else if(requestCode == ActivityJumpUtil.RequestCode.SHA_WIFI_SHARE_2_SCAN){
            //从添加Wifi的wifi扫描处返回
        }
        mainSharePS.getMySharedWifiList();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shaMySharedRefreshBtn:
                mainSharePS.getMySharedWifiList();
                break;
            case R.id.shaMySharedAddWifiBtn:
                ActivityJumpUtil.jump2SHAWifiScanAct(getActivity());
                break;
            default:
                break;
        }
    }
}
